package com.test.qpbgboot.evaluation.service.impl;

import com.test.qpbgboot.base.enums.OrderStatusEnum;
import com.test.qpbgboot.base.exception.ServiceException;
import com.test.qpbgboot.base.response.ResultStatus;
import com.test.qpbgboot.evaluation.mapper.EscortEvaluationMapper;
import com.test.qpbgboot.evaluation.mapper.OrderEvaluationMapper;
import com.test.qpbgboot.evaluation.pojo.dto.EscortEvaluationQuery;
import com.test.qpbgboot.evaluation.pojo.dto.EscortEvaluationSaveParam;
import com.test.qpbgboot.evaluation.pojo.dto.OrderEvaluationQuery;
import com.test.qpbgboot.evaluation.pojo.dto.OrderEvaluationSaveParam;
import com.test.qpbgboot.evaluation.pojo.entity.EscortEvaluation;
import com.test.qpbgboot.evaluation.pojo.entity.OrderEvaluation;
import com.test.qpbgboot.evaluation.pojo.vo.EscortEvaluationVO;
import com.test.qpbgboot.evaluation.pojo.vo.OrderEvaluationVO;
import com.test.qpbgboot.evaluation.service.EvaluationService;
import com.test.qpbgboot.schedule.mapper.OrderMapper;
import com.test.qpbgboot.schedule.pojo.dto.OrderQuery;
import com.test.qpbgboot.schedule.pojo.entity.OrderInfo;
import com.test.qpbgboot.schedule.pojo.vo.OrderInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class EvaluationServiceImpl implements EvaluationService {

    @Autowired
    private EscortEvaluationMapper escortEvaluationMapper;
    @Autowired
    private OrderEvaluationMapper orderEvaluationMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<EscortEvaluationVO> escortEvaluationSelect(
            EscortEvaluationQuery escortEvaluationQuery) {
        log.debug("陪玩评价查询业务，参数:{}", escortEvaluationQuery);
        List<EscortEvaluationVO> escortEvaluationVOList =
                escortEvaluationMapper
                        .selectEscortEvaluationVObyEscortEvaluationQuery(
                                escortEvaluationQuery);
        log.debug("陪玩评价查询结果:{}", escortEvaluationVOList);
        return escortEvaluationVOList;
    }

    @Override
    public void escortEvaluationSave(EscortEvaluationSaveParam escortEvaluationSaveParam) {
        log.debug("陪玩评价保存业务，参数:{}", escortEvaluationSaveParam);
        Long escortId = escortEvaluationSaveParam.getEscortId();
        Long orderId = escortEvaluationSaveParam.getOrderId();

        checkAndUpdateOrder(orderId, escortId);

        EscortEvaluationQuery escortEvaluationQuery =
                new EscortEvaluationQuery();
        escortEvaluationQuery.setEscortId(escortId);
        escortEvaluationQuery.setOrderId(orderId);
        List<EscortEvaluationVO> escortEvaluationVOList = escortEvaluationMapper
                .selectEscortEvaluationVObyEscortEvaluationQuery(escortEvaluationQuery);

        EscortEvaluation escortEvaluation = new EscortEvaluation();
        BeanUtils.copyProperties(escortEvaluationSaveParam, escortEvaluation);

        if (escortEvaluationVOList == null || escortEvaluationVOList.isEmpty()) {
            escortEvaluationMapper.insert(escortEvaluation);
        } else if (escortEvaluationVOList.size() == 1) {
            escortEvaluation.setId(escortEvaluationVOList.get(0).getId());
            escortEvaluationMapper.update(escortEvaluation);
        } else {
            throw new ServiceException(ResultStatus.DIRTY_DATA, "多条陪玩评论数据");
        }
    }

    @Override
    public void escortEvaluationDelete(Long id) {
        log.debug("陪玩评价删除业务，参数:{}", id);
        escortEvaluationMapper.deleteById(id);
    }

    @Override
    public List<OrderEvaluationVO> orderEvaluationSelect(
            OrderEvaluationQuery orderEvaluationQuery) {
        log.debug("订单查询业务，参数:{}", orderEvaluationQuery);
        List<OrderEvaluationVO> orderEvaluationVOList =
                orderEvaluationMapper
                        .selectOrderEvaluationVObyOrderEvaluationQuery(
                                orderEvaluationQuery);
        log.debug("订单查询结果:{}", orderEvaluationVOList);
        return orderEvaluationVOList;
    }

    @Override
    public void orderEvaluationSave(OrderEvaluationSaveParam orderEvaluationSaveParam) {
        log.debug("订单评价保存业务，参数:{}", orderEvaluationSaveParam);
        Long orderId = orderEvaluationSaveParam.getOrderId();
        checkAndUpdateOrder(orderId, null);

        OrderEvaluationQuery orderEvaluationQuery =
                new OrderEvaluationQuery();
        orderEvaluationQuery.setOrderId(orderId);
        List<OrderEvaluationVO> orderEvaluationVOList =
                orderEvaluationMapper
                        .selectOrderEvaluationVObyOrderEvaluationQuery(
                                orderEvaluationQuery);

        OrderEvaluation orderEvaluation = new OrderEvaluation();
        BeanUtils.copyProperties(orderEvaluationSaveParam, orderEvaluation);

        if (orderEvaluationVOList == null || orderEvaluationVOList.isEmpty()) {
            orderEvaluationMapper.insert(orderEvaluation);
        } else if (orderEvaluationVOList.size() == 1) {
            orderEvaluation.setId(orderEvaluationVOList.get(0).getId());
            orderEvaluationMapper.update(orderEvaluation);
        } else {
            throw new ServiceException(ResultStatus.DIRTY_DATA, "多条订单评论数据");
        }
    }

    @Override
    public void orderEvaluationDelete(Long id) {
        log.debug("订单评价删除业务，参数:{}", id);
        orderEvaluationMapper.deleteById(id);
    }

    /**
     * 检查、更新订单状态以及是否与该陪玩相关
     */
    private void checkAndUpdateOrder(Long orderId, Long escortId) {
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.setId(orderId);
        List<OrderInfoVO> orderInfoVOList =
                orderMapper.selectOrderInfoVObyOrderQuery(orderQuery);
        if (orderInfoVOList == null || orderInfoVOList.isEmpty()) {
            throw new ServiceException(ResultStatus.INVALID_ID, "订单id无效");
        }
        OrderInfoVO orderInfoVO = orderInfoVOList.get(0);
        String orderStatus = orderInfoVO.getOrderStatus();
        if (orderStatus.equals(OrderStatusEnum.ATTENDED.getValue())) {
            if (Objects.equals(orderInfoVO.getAppointmentDate(), LocalDate.now())
                    && LocalTime.now().isBefore(orderInfoVO.getEndTime())) {
                throw new ServiceException(ResultStatus.INCORRECT_STATUS, "预定时间结束后才可以评价");
            } else {
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setId(orderId);
                orderInfo.setOrderStatus(OrderStatusEnum.EVALUATED.getValue());
                orderMapper.changeStatusAndMessage(orderInfo);
            }
        } else if (!orderStatus.equals(OrderStatusEnum.EVALUATED.getValue())) {
            throw new ServiceException(ResultStatus.INCORRECT_STATUS, "订单状态错误");
        }
        List<Long> escortIdList = orderInfoVO.getEscortIdList();
        if (escortId != null) {
            if (escortIdList == null || !escortIdList.contains(escortId)) {
                throw new ServiceException(ResultStatus.INVALID_ID, "陪玩id不在订单中");
            }
        }
    }
}
